package com.leetcode.algorithm.y22.m07.w4;

/**
 * 1184. 公交站间的距离
 * 
 * https://leetcode.cn/problems/distance-between-bus-stops/
 * 
 * @author jie.deng
 *
 */
class Question1184Solution01 {

	public int distanceBetweenBusStops(int[] distance, int start, int destination) {
		if (start == destination) {
			return 0;
		}
		int left = Math.min(start, destination);
		int right = Math.max(start, destination);
		int sum1 = 0;
		int idx = left;
		while (idx < right) {
			sum1 += distance[idx];
			idx++;
		}

		int sum2 = 0;
		idx = right;
		while (idx < distance.length) {
			sum2 += distance[idx];
			idx++;
		}
		idx = 0;
		while (idx < left) {
			sum2 += distance[idx];
			idx++;
		}

		return Math.min(sum1, sum2);
	}

}